#include <cstdio>
#include <cctype>
#include <cstring>

const int MAX_INT = 0x6F6F6F6F;

struct min_cost {
	int V, C;
	min_cost(int v, int c): V(v), C(c) {}
	min_cost(): V(0), C(0) {}
};

int N;
int G[30][30];
bool VIS[30];
min_cost MC[30];

int prim() {
	int route = 0;
	memset(VIS, 0, sizeof(VIS));
	for (int i=1; i<=N; i++) {
		MC[i] = min_cost(1, G[1][i]);
	}
	VIS[1] = true;
	for (int i=1; i<N; i++) {
		int v, cost=MAX_INT;
		for (int j=1; j<=N; j++) {
			if (!VIS[j] && MC[j].C<cost) {
				v=j;
				cost=MC[j].C;
			}
		}
		route+=cost;
		VIS[v] = true;
		for (int j=1; j<=N; j++) {
			if (!VIS[j] && G[v][j]<MC[j].C) {
				MC[j].V = v;
				MC[j].C = G[v][j];
			}
		}
	}
	return route;
}

int main() {
	while (true) {
		scanf("%d", &N);
		if (N==0) break;
		memset(G, 0x6F, sizeof(G));
		for (int i=1; i<N; i++) {
			char c[5];
			int k;
			scanf("%s%d", c, &k);
			int v = toupper(c[0]) - 'A' + 1;
			for (int j=0; j<k; j++) {
				char c2[5];
				int cost;
				scanf("%s%d", c2, &cost);
				int v2 = toupper(c2[0]) - 'A' + 1;
				G[v][v2] = cost;
				G[v2][v] = cost;
			}
		}
		int min = prim();
		printf("%d\n", min);
	}
	return 0;
}
